﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace ArchSoft
{
    public partial class PrInfFrm : Form
    {
        //Global Variables use in Form

        //Project
        string ProjectName;
        DateTime StartDate;
        DateTime EndDate;
        string ServiceRequested;
        string Owner;
        string Category;
        string Logo;
        string Value;
        string Tel;
        string Fax;
        string Location;
        string Email;
        string Website;
        string ProjectDescription;

        //Consultants
        string ConsultantsName;
        string ConsultantsAddress;
        string ConsultantsTelNo;
        string ConsultantsFaxNo;
        string ConsultantsWebsite;

        //Contacts
        string ContactName;
        string ContactPosition;
        string ContactEmail;
        string ContactOfficeNo;
        string ContactCellNo;

        string Commnent;

        //ProjectPath
        string _ProjectPath;
        
        //Global DataTable and DataRow
        DataTable dt = new DataTable();
        DataRow row;


        public PrInfFrm()
        {
            InitializeComponent();

            //Initialize DataTable and DataColummn
            dt = new DataTable("Project Information");
            dt.Columns.Add("ProjectName");
            dt.Columns.Add("StartDate");
            dt.Columns.Add("EndDate");
            dt.Columns.Add("ServiceRequested");
            dt.Columns.Add("Owner");
            dt.Columns.Add("Category");
            dt.Columns.Add("Logo");
            dt.Columns.Add("Value");
            dt.Columns.Add("Tel");
            dt.Columns.Add("Fax");
            dt.Columns.Add("Location");
            dt.Columns.Add("Email");
            dt.Columns.Add("WebSite");
            dt.Columns.Add("ProjectDescription");

            dt.Columns.Add("ConsultantsName");
            dt.Columns.Add("ConsultantsAddress");
            dt.Columns.Add("ConsultantsTelNo");
            dt.Columns.Add("ConsultantsFaxNo");
            dt.Columns.Add("ConsultantsWebsite");

            dt.Columns.Add("ContactName");
            dt.Columns.Add("ContactPosition");
            dt.Columns.Add("ContactEmail");
            dt.Columns.Add("ContactOfficeNo");
            dt.Columns.Add("ContactCellNo");

            dt.Columns.Add("Commnent");
        }

        //ProjectPath Properties
        public string ProjectPath
        {
            set { _ProjectPath = value; }
            get { return _ProjectPath; }
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            AssignedVariable();
            AddData();
            WriteToXML();
        }

        private void WriteToXML()
        {
            try
            {
                //Write to XML
                DataSet ds = new DataSet();
                string path = _ProjectPath + Path.DirectorySeparatorChar + "ProjectInformation.PI";
                ds.Tables.Add(dt);
                ds.WriteXml(path);
                ds.Dispose();
            }
            catch (Exception ex)
            {

            }
        }

        private void AssignedVariable()
        {
            try
            {
                ProjectName = txtProName.Text;
                StartDate = dateTimePickerStart.Value;
                EndDate = dateTimePickerEnd.Value;
                ServiceRequested = txtSerRe.Text;
                Owner = txtOwner.Text;
                Category = comboBoxCateLog.SelectedItem.ToString();
                Logo = txtLogo.Text;
                Value = txtValue.Text;
                Tel = txtTel.Text;
                Fax = txtFax.Text;
                Location = comboBoxLocation.SelectedItem.ToString();
                Email = txtEmail.Text;
                Website = txtWebsite.Text;
                ProjectDescription = txtProDes.Text;

                ConsultantsName = txtNameC.Text;
                ConsultantsAddress = txtAddressC.Text;
                ConsultantsTelNo = txtTelC.Text;
                ConsultantsFaxNo = txtFaxC.Text;
                ConsultantsWebsite = txtWebsiteC.Text;

                ContactName = txtNameC.Text;
                ContactPosition = txtPosP.Text;
                ContactEmail = txtEmailP.Text;
                ContactOfficeNo = txtOffP.Text;
                ContactCellNo = txtCellNoP.Text;

                Commnent = txtComment.Text;
            }
            catch (Exception ex)
            {

            }
        }

        private void AddData()
        {
            try
            {
                //Assign value to DataRow in DataTable
                row = dt.NewRow();

                row["ProjectName"] = ProjectName;
                row["StartDate"] = StartDate;
                row["EndDate"] = EndDate;
                row["ServiceRequested"] = ServiceRequested;
                row["Owner"] = Owner;
                row["Category"] = Category;
                row["Logo"] = Logo;
                row["Value"] = Value;
                row["Tel"] = Tel;
                row["Fax"] = Fax;
                row["Location"] = Location;
                row["Email"] = Email;
                row["Website"] = Website;
                row["ProjectDescription"] = ProjectDescription;

                row["ConsultantsName"] = ConsultantsName;
                row["ConsultantsAddress"] = ConsultantsAddress;
                row["ConsultantsTelNo"] = ConsultantsTelNo;
                row["ConsultantsFaxNo"] = ConsultantsFaxNo;
                row["ConsultantsWebsite"] = ConsultantsWebsite;

                row["ContactName"] = ContactName;
                row["ContactPosition"] = ContactPosition;
                row["ContactEmail"] = ContactEmail;
                row["ContactOfficeNo"] = ContactOfficeNo;
                row["ContactCellNo"] = ContactCellNo;

                row["Commnent"] = Commnent;

                dt.Rows.Add(row);
            }
            catch (Exception ex)
            {

            }
        }

        private void PrInfFrm_Load(object sender, EventArgs e)
        {
            try
            {
                //


                //
                string path = _ProjectPath + Path.DirectorySeparatorChar + "ProjectInformation.PI";
                if (File.Exists(path))
                {
                    DataSet ds2 = new DataSet();
                    ds2.ReadXml(path);
                    DataTable temp = new DataTable();
                    temp = ds2.Tables[0].Copy();

                    for (int i = 0; i < temp.Rows.Count; i++)
                    {
                        ProjectName = temp.Rows[i]["ProjectName"].ToString();
                        //
                        string  VStartDate = temp.Rows[i]["StartDate"].ToString();
                        string VEndDate = temp.Rows[i]["EndDate"].ToString();
                        StartDate =  Convert.ToDateTime(VStartDate);
                        EndDate = Convert.ToDateTime(VEndDate);
                        //
                        ServiceRequested = temp.Rows[i]["ServiceRequested"].ToString();
                        Owner = temp.Rows[i]["Owner"].ToString();
                        Category = temp.Rows[i]["Category"].ToString();
                        Logo = temp.Rows[i]["Logo"].ToString();
                        Value = temp.Rows[i]["Value"].ToString();
                        Tel = temp.Rows[i]["Tel"].ToString();
                        Fax = temp.Rows[i]["Fax"].ToString();
                        Location = temp.Rows[i]["Location"].ToString();
                        Email = temp.Rows[i]["Email"].ToString();
                        Website = temp.Rows[i]["Website"].ToString();
                        ProjectDescription = temp.Rows[i]["ProjectDescription"].ToString();

                        ConsultantsName = temp.Rows[i]["ConsultantsName"].ToString();
                        ConsultantsAddress = temp.Rows[i]["ConsultantsAddress"].ToString();
                        ConsultantsTelNo = temp.Rows[i]["ConsultantsTelNo"].ToString();
                        ConsultantsFaxNo = temp.Rows[i]["ConsultantsFaxNo"].ToString();
                        ConsultantsWebsite = temp.Rows[i]["ConsultantsWebsite"].ToString();

                        ContactName = temp.Rows[i]["ContactName"].ToString();
                        ContactPosition = temp.Rows[i]["ContactPosition"].ToString();
                        ContactEmail = temp.Rows[i]["ContactEmail"].ToString();
                        ContactOfficeNo = temp.Rows[i]["ContactOfficeNo"].ToString();
                        ContactCellNo = temp.Rows[i]["ContactCellNo"].ToString();

                        Commnent = temp.Rows[i]["Commnent"].ToString();

                        setText();
                    }
                }
                else
                {
                    string[] temp = _ProjectPath.Split(Path.DirectorySeparatorChar);
                    string projectName = temp[temp.Length - 1].ToString();
                    txtProName.Text = projectName;
                    txtProName.Enabled = false;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void setText()
        {
            txtProName.Text = ProjectName;
            dateTimePickerStart.Value = StartDate;
            dateTimePickerEnd.Value = EndDate;
            txtSerRe.Text = ServiceRequested;
            txtOwner.Text=Owner;
            comboBoxCateLog.SelectedItem = Category;
            txtLogo.Text = Logo;
            txtValue.Text = Value ;
            txtTel.Text = Tel;
            txtFax.Text = Fax;
            comboBoxLocation.SelectedItem = Location;
            txtEmail.Text = Email;
            txtWebsite.Text = Website;
            txtProDes.Text = ProjectDescription;

            txtNameC.Text = ConsultantsName;
            txtAddressC.Text = ConsultantsAddress;
            txtTelC.Text = ConsultantsTelNo;
            txtFaxC.Text = ConsultantsFaxNo;
            txtWebsiteC.Text = ConsultantsWebsite;

            txtNameP.Text = ContactName;
            txtPosP.Text = ContactPosition;
            txtEmailP.Text = ContactEmail;
            txtOffP.Text = ContactOfficeNo;
            txtCellNoP.Text = ContactCellNo;

            txtComment.Text = Commnent;
        }

        private void txtTel_Leave(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtTel.Text, "\\d+"))
            {
                //MessageBox.Show("You must input a number");
                MessageBox.Show("Bạn phải nhập một số ");
                txtTel.Focus();
                txtTel.SelectAll();
            }
        }

        private void txtFax_Leave(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtFax.Text, "\\d+"))
            {
                //MessageBox.Show("You must input a number");
                MessageBox.Show("Bạn phải nhập một số ");
                txtFax.Focus();
                txtFax.SelectAll();
            }
        }

        private void txtTelC_Leave(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtTelC.Text, "\\d+"))
            {
                //MessageBox.Show("You must input a number");
                MessageBox.Show("Bạn phải nhập một số ");
                txtTelC.Focus();
                txtTelC.SelectAll();
            }
        }

        private void txtFaxC_Leave(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtFaxC.Text, "\\d+"))
            {
                //MessageBox.Show("You must input a number");
                MessageBox.Show("Bạn phải nhập một số ");
                txtFaxC.Focus();
                txtFaxC.SelectAll();
            }
        }

        private void txtCellNoP_Leave(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtCellNoP.Text, "\\d+"))
            {
                //MessageBox.Show("You must input a number");
                MessageBox.Show("Bạn phải nhập một số ");
                txtCellNoP.Focus();
                txtCellNoP.SelectAll();
            }
        }

        public bool ValidateEmailAddress(string emailAddress)
        {
            try
            {
                string TextToValidate = emailAddress;
                Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
                Match m = emailregex.Match(TextToValidate);
                if (m.Success)
                {
                    // success,, so do something;
                    return true;
                }
                else
                {
                    // didn't manage to find the regular expression
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void txtEmail_Leave(object sender, EventArgs e)
        {
           if(!ValidateEmailAddress(txtEmail.Text))
           {
               //MessageBox.Show("Invalid email address");
               MessageBox.Show("Địa chỉ email không hợp lệ ");
               txtEmail.SelectAll();
               txtEmail.Focus();
           }
        }

        private void txtEmailP_Leave(object sender, EventArgs e)
        {
            if (!ValidateEmailAddress(txtEmailP.Text))
            {
                //MessageBox.Show("Invalid email address");
                MessageBox.Show("Địa chỉ email không hợp lệ ");
                txtEmailP.SelectAll();
                txtEmailP.Focus();
            }
        }

        private void buttonReport_Click(object sender, EventArgs e)
        {
            string path = _ProjectPath + Path.DirectorySeparatorChar + "ProjectInformation.PI";
            PrInfFrmReport frm = new PrInfFrmReport();
            frm.FilePath = path;
            frm.LoadData();
            frm.WindowState = FormWindowState.Maximized;
            frm.ShowDialog();

        }
    }
}